package com.otp.service.instance.impl;

import org.appfuse.model.User;
import org.appfuse.service.UserExistsException;
import org.appfuse.service.impl.GenericManagerImpl;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;

import com.otp.exceptions.EmailExistsException;
import com.otp.model.user.dao.User1Dao;
import com.otp.service.instance.User1Manager;

public class User1ManagerImpl extends GenericManagerImpl<User,Long> implements User1Manager{

	private User1Dao user1Dao;

	@Autowired
	public void setUser1Dao(User1Dao user1Dao){
		this.dao = user1Dao;
		this.user1Dao = user1Dao;
	}
	
	public User getUserByUsername(String username){
		User result=null;
		try {
			result = (User) user1Dao.getUserByUsername(username);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public User getUserByEmail(String email){
		User result=null;
		try {
			result = (User) user1Dao.getUserByEmail(email);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public void updateUser(User user) throws EmailExistsException{
		try{
			user1Dao.updateUser(user);
		}
		catch (ConstraintViolationException e){
			throw new EmailExistsException("User '" + user.getUsername() + "' already exists!");
		}
		catch  (Exception e){
			e.printStackTrace();
		}
	}
	
	public void addUser(User user) throws UserExistsException{
		try{
			user1Dao.addUser(user);
		}
		catch (ConstraintViolationException e){
			throw new UserExistsException("User '" + user.getUsername() + "' already exists!");
		}
		catch  (Exception e){
			e.printStackTrace();
		}
	}
}
